home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- #include <stdio.h>
- #include <string.h>
- #include <time.h>
- #include <sys/time.h>
- #include <sys/types.h>
- #include "Utils.h"
-
- static char *smonth[13]= {
- NULL, "January", "February", "March", "April", "May", "June", "July",
- "August", "September", "October", "November", "December",
- };
-
- static char *sweek[8]= {
- NULL, "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
- "Saturday",
- };
-
- int
- isWhiteSpace(char ch)
- {
- return (ch == ' ' || ch == ',' || ch == '\t');
- }
-
- static char *
- skipWhiteSpace(char *ptr)
- {
- while (isWhiteSpace(*ptr)) {
- ptr++;
- }
- return ptr;
- }
-
- static char *
- parseWord(char *ptr, char *str_return)
- {
- while (!isWhiteSpace(*ptr) && *ptr != '\0') {
- *str_return = *ptr;
- ptr++;
- str_return++;
- }
- *str_return = '\0';
- return skipWhiteSpace(ptr);
- }
-
- static char *
- parseNumber(char *ptr, int *num_return)
- {
- char dummy[256];
-
- if (sscanf(ptr, "%d", num_return) == 1) {
- return parseWord(ptr, dummy);
- } else {
- return NULL;
- }
- }
-
- /**********************************************************************/
-
- void
- formatTime(int hour, int min, int clock24, char *str_return)
- {
- if (clock24) {
- sprintf(str_return, "%d:", hour);
- if (min < 10) {
- strcat(str_return, "0");
- }
- sprintf(str_return+strlen(str_return), "%d", min);
- } else {
- formatShortTime(hour, min, str_return);
- if (hour >= 12) {
- strcat(str_return, "pm");
- } else {
- strcat(str_return, "am");
- }
- }
- }
-
- void
- formatShortTime(int hour, int min, char *str_return)
- {
- if (hour % 12 == 0) {
- sprintf(str_return, "12:");
- } else {
- sprintf(str_return, "%d:", hour % 12);
- }
- if (min < 10) {
- strcat(str_return, "0");
- }
- sprintf(str_return+strlen(str_return), "%d", min);
- }
-
- void
- formatDate(int weekday, int day, int month, int year, char *str_return)
- {
- if (weekday && month) {
- if (year) {
- sprintf(str_return, "%s, %s %d, %d", sweek[weekday], smonth[month],
- day, year);
- } else {
- sprintf(str_return, "%s, %s %d", sweek[weekday], smonth[month],
- day);
- }
- } else {
- strcpy(str_return, "");
- }
- }
-
- void
- formatDate(int day, int month, int year, char *str_return)
- {
- if (month) {
- if (year) {
- sprintf(str_return, "%s %d, %d", smonth[month], day, year);
- } else {
- sprintf(str_return, "%s %d", smonth[month], day);
- }
- } else {
- strcpy(str_return, "");
- }
- }
-
- void
- formatDate(int month, int year, char *str_return)
- {
- if (month) {
- sprintf(str_return, "%s %d", smonth[month], year);
- } else {
- strcpy(str_return, "");
- }
- }
-
- int
- readInt(FILE *fd, int *num)
- {
- static char str[MAXSTR];
-
- return (fgets(str, sizeof(str), fd) && (sscanf(str, "%d", num) == 1));
- }
-
- void
- writeInt(FILE *fd, int num, char *annotation)
- {
- static char str[20];
- int each;
-
- if (annotation) {
- sprintf(str, "%d", num);
- for (each=strlen(str); each<sizeof(str)-1; each++) {
- str[each] = ' ';
- }
- str[sizeof(str)-1] = '\0';
- fprintf(fd, "%s%s\n", str, annotation);
- } else {
- fprintf(fd, "%d\n", num);
- }
- }
-
- int
- readStr(FILE *fd, char *str_return)
- {
- static char str[MAXSTR];
- int len, str_ct, each;
-
- if (!fgets(str, sizeof(str), fd)) {
- return 0;
- } else {
- len = strlen(str);
- if (str[len-1] == '\n') {
- str[len-1] = '\0';
- len--;
- }
- str_ct = 0;
- for (each=0; each<len; each++) {
- if (str[each] == '\\' && each < len-1 && str[each+1] == 'n') {
- str_return[str_ct] = '\n';
- each++;
- } else {
- str_return[str_ct] = str[each];
- }
- str_ct++;
- }
- str_return[str_ct] = '\0';
- return 1;
- }
- }
-
- void
- writeStr(FILE *fd, char *str)
- {
- int each, len;
-
- len = strlen(str);
- if (len > MAXSTR/2) {
- len = MAXSTR/2;
- }
- for (each=0; each<len; each++) {
- if (str[each] == '\n') {
- fprintf(fd, "\\n");
- } else {
- fprintf(fd, "%c", str[each]);
- }
- }
- fprintf(fd, "\n");
- }
-
- int
- parseDate(char *str, int *day, int *month, int *year)
- {
- char *slash1, *slash2, *ptr, token[256];
- struct tm *now;
- struct timeval t;
-
- gettimeofday(&t, NULL);
- now = localtime(&t.tv_sec);
- if (!strlen(str)) {
- *day = *month = *year = 0;
- return 1;
- } else if (!strcasecmp(str, "today")) {
- *day = now->tm_mday;
- *month = now->tm_mon+1;
- *year = now->tm_year+1900;
- return 1;
- } else if ((slash1 = strchr(str, '/')) &&
- (slash2 = strrchr(str, '/'))) {
- if (slash1 != slash2 &&
- strchr(slash1+1, '/') == slash2) {
- if (sscanf(str, "%d", month) == 1 &&
- sscanf(slash1+1, "%d", day) == 1 &&
- sscanf(slash2+1, "%d", year) == 1) {
- if (*year < 100) {
- *year += 1900;
- }
- return 1;
- }
- } else if (slash1 == slash2) {
- if (sscanf(str, "%d", month) == 1 &&
- sscanf(slash1+1, "%d", day) == 1) {
- *year = now->tm_year+1900;
- return 1;
- }
- }
- return 0;
- } else {
- ptr = skipWhiteSpace(str);
- if (!ptr) {
- return 0;
- }
- ptr = parseWord(ptr, token);
- *month = matchMonthString(token);
- if (!ptr || !(*month)) {
- return 0;
- }
- ptr = parseNumber(ptr, day);
- if (!ptr || *day < 1) {
- return 0;
- }
- if (*ptr == '\0') {
- *year = now->tm_year+1900;
- return 1;
- }
- ptr = parseNumber(ptr, year);
- if (!ptr || *ptr != '\0') {
- return 0;
- }
- if (*year < 100) {
- *year += 1900;
- }
- if (*day > NumberOfDays(*month, *year)) {
- return 0;
- }
- return 1;
- }
- }
-
- int
- parseMonth(char *str, int *month, int *year)
- {
- int day;
- char *slash1, *slash2, *ptr, token[256];
- struct tm *now;
- struct timeval t;
-
- gettimeofday(&t, NULL);
- now = localtime(&t.tv_sec);
- if (parseDate(str, &day, month, year)) {
- return 1;
- } else if (!strlen(str)) {
- *month = *year = 0;
- return 1;
- } else if ((slash1 = strchr(str, '/')) &&
- (slash2 = strrchr(str, '/'))) {
- if (slash1 != slash2 &&
- strchr(slash1+1, '/') == slash2) {
- if (sscanf(str, "%d", month) == 1 &&
- sscanf(slash1+1, "%d", &day) == 1 &&
- sscanf(slash2+1, "%d", year) == 1) {
- if (*year < 100) {
- *year += 1900;
- }
- return 1;
- }
- } else if (slash1 == slash2) {
- if (sscanf(str, "%d", month) == 1 &&
- sscanf(slash1+1, "%d", year) == 1) {
- if (*year < 100) {
- *year += 1900;
- }
- return 1;
- }
- }
- return 0;
- } else {
- ptr = skipWhiteSpace(str);
- if (!ptr) {
- return 0;
- }
- ptr = parseWord(ptr, token);
- *month = matchMonthString(token);
- if (!ptr || !(*month)) {
- return 0;
- }
- if (*ptr == '\0') {
- *year = now->tm_year+1900;
- return 1;
- }
- ptr = parseNumber(ptr, year);
- if (!ptr || *ptr != '\0') {
- return 0;
- }
- if (*year < 100) {
- *year += 1900;
- }
- return 1;
- }
- }
-
- int
- compareDates(int day1, int month1, int year1, int day2, int month2, int year2)
- {
- if (year1 < year2) {
- return -1;
- } else if (year1 > year2) {
- return 1;
- }
- if (month1 < month2) {
- return -1;
- } else if (month1 > month2) {
- return 1;
- }
- if (day1 < day2) {
- return -1;
- } else if (day1 > day2) {
- return 1;
- }
- return 0;
- }
-
- int
- computeWeekday(int day, int month, int year)
- {
- static int lastFirst, lastMonth = 0, lastYear = 0;
-
- if (month != lastMonth || year != lastYear) {
- lastFirst = FirstDay(month, year);
- lastMonth = month;
- lastYear = year;
- }
- return ((day+lastFirst-2) % 7)+1;
- }
-
- int
- computeDayDifference(int day1, int month1, int year1,
- int day2, int month2, int year2)
- {
- int diff, m, y, num;
-
- diff = 0;
- num = NumberOfDays(month2, year2);
- if (year1 > year2 || month1 > month2) {
- diff += num-day2;
- } else if (day1 > day2) {
- diff += day1-day2;
- }
- if (year1 > year2) {
- for (m=month2+1; m<=12; m++) {
- diff += NumberOfDays(m, year2);
- }
- } else {
- for (m=month2+1; m<month1; m++) {
- diff += NumberOfDays(m, year2);
- }
- }
- for (y=year2+1; y<year1; y++) {
- if (y % 400 == 0) {
- diff += 366;
- } else if (y % 100 == 0) {
- diff += 365;
- } else if (y % 4 == 0) {
- diff += 366;
- } else {
- diff += 365;
- }
- }
- if (year1 > year2) {
- for (m=1; m<month1; m++) {
- diff += NumberOfDays(m, year1);
- }
- }
- if (year1 > year2 || month1 > month2) {
- diff += day1;
- }
- return diff;
- }
-
- void
- augmentDate(int day, int month, int year, int days, int *d, int *m, int *y)
- {
- *d = day;
- *m = month;
- *y = year;
- while (days > NumberOfDays(*m, *y)-*d) {
- days -= NumberOfDays(*m, *y)-*d;
- *d = 0;
- (*m)++;
- if (*m == 12) {
- *m = 0;
- (*y)++;
- }
- }
- (*d) += days;
- }
-
- void
- decrementDate(int day, int month, int year, int days, int *d, int *m, int *y)
- {
- *d = day;
- *m = month;
- *y = year;
-
- if (*d > days) {
- *d -= days;
- } else {
- days -= *d;
- if (!*m) {
- *m = 12;
- (*y)--;
- } else {
- (*m)--;
- }
- *d = NumberOfDays(*m, *y)-days;
- // Oops, what if days > # days in month????
- }
- }
-
- int
- matchMonthString(char *str)
- {
- int each;
-
- for (each=1; each<=12; each++) {
- if (!strcasecmp(str, smonth[each])) {
- return each;
- }
- }
- // Now try just the first three letters
- if (strlen(str) == 3) {
- for (each=1; each<=12; each++) {
- if (!strncasecmp(str, smonth[each], 3)) {
- return each;
- }
- }
- }
- return 0;
- }
-
- char *
- monthString(int month)
- {
- return smonth[month];
- }
-
- char *
- weekdayString(int day)
- {
- return sweek[day];
- }
-
- /**********************************************************************/
-
- //
- // Taken from xcalendar source
- //
-
- static char mon[] = {
- 0,
- 31, 29, 31, 30,
- 31, 30, 31, 31,
- 30, 31, 30, 31,
- };
-
- static int calInit = 0;
-
- /*
- * return day of the week
- * of jan 1 of given year
- */
-
- int
- jan1(int yr)
- {
- register y, d;
-
- /*
- * normal gregorian calendar
- * one extra day per four years
- */
-
- y = yr;
- d = 4+y+(y+3)/4;
-
- /*
- * julian calendar
- * regular gregorian
- * less three days per 400
- */
-
- if(y > 1800) {
- d -= (y-1701)/100;
- d += (y-1601)/400;
- }
-
- /*
- * great calendar changeover instant
- */
-
- if(y > 1752)
- d += 3;
-
- return(d%7);
- }
-
- /* should be called first */
- int
- FirstDay(int m, int y)
- {
- register d, i;
-
- calInit = y;
- d = jan1(y);
- mon[2] = 29;
- mon[9] = 30;
-
- switch((jan1(y+1)+7-d)%7) {
-
- /*
- * non-leap year
- */
- case 1:
- mon[2] = 28;
- break;
-
- /*
- * 1752
- */
- default:
- mon[9] = 19;
- break;
-
- /*
- * leap year
- */
- case 2:
- ;
- }
-
- for(i=1; i<m; i++)
- d += mon[i];
- d %= 7;
-
- d = d>0 ? d : 7; /* returns 1-7, not 0-6 */
-
- d = d + 1 - 0; // d = d + 1 - appResources.firstDay;
- return d>0 ? d : 7+d;
- }
-
- int
- NumberOfDays(int m, int y)
- {
- if(calInit != y)
- FirstDay(m,y);
- return mon[m];
- }
-
-